const express = require("express");
const axios = require("axios");

const app = express(); // 创建一个express实例

app.use(async (req, res, next) => {
  console.log("express middleware 1");
  req.msg = "hello middleware01";
  await next();

  // res.json(req.msg); // next()之后才会执行
});

app.use(async (req, res, next) => {
  console.log("express middleware 2");
  req.msg += " middleware02";
  await next();
});

app.use(async (req, res, next) => {
  console.log("express middleware 3");
  const resData = await axios.get("http://123.207.32.32:8000/home/multidata");
  req.msg += resData.data.data.banner.list[0].title;

  res.json(req.msg); // 只能在这里返回结果 next()之后才会执行
});

app.listen(8000, () => {
  console.log("express server running at http://127.0.0.1:8000");
});
